package com.xj.zhihudemo.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.pnikosis.materialishprogress.ProgressWheel;
import com.xj.zhihudemo.R;
import com.xj.zhihudemo.mode.LastNewsInfo;
import com.xj.zhihudemo.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/18.
 */

public class TestMainAdapter extends RecyclerView.Adapter<TestMainAdapter.ViewHolder> {

    //上拉加载更多
    public static final int PULL_UP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    private static final String LOG_TAG = TestMainAdapter.class.getSimpleName();
    //------------------------------------
    //-----用于区分是否是头部或者尾部-------
    //------------------------------------
    private static final int HEADER_ITEM = 2;
    private static final int LOAD_MORE_ITEM = 3;
    //----------------------------------
    //------用于区分Item的类型-----------
    // ---------------------------------
    private static final int NORMAL_ITEM = 4;
    private static final int GROUP_ITEM = 5;
    //上拉加载更多状态-默认为0
    private int load_more_status = 0;
    //一些变量
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private LastNewsInfo mLastNewsInfo;
    private List<LastNewsInfo.StoriesEntity> mLastNewsData;
//    private MainNewsAdapter.OnItemClickHelper mOnItemClickHelper;

    private ArrayList<Integer> mIdList;

    private boolean bIsLoading = false;
    private View headerView;

    /**
     * 构造方法
     */
    public TestMainAdapter(Context context) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mLastNewsData = new ArrayList<LastNewsInfo.StoriesEntity>();
        mIdList = new ArrayList<>();
    }

    /**
     * @param header 添加头部
     */
    public void addHeader(View header) {
        this.headerView = header;
        notifyDataSetChanged();
    }

    /**
     * @param mData 添加数据
     */
    public void addData(LastNewsInfo mData) {

        mLastNewsInfo = mData;
        mLastNewsData.clear();
        mLastNewsData.addAll(mData.getStories());
        if (mLastNewsData != null) {
            mIdList.clear();
            for (LastNewsInfo.StoriesEntity storiesEntity : mLastNewsData) {
                mIdList.add(storiesEntity.getId());
            }
        }
        notifyDataSetChanged();
    }


    public void addMoreItem(List<String> newDatas) {
//        mTitles.addAll(newDatas);
        Log.d(LOG_TAG, "加载更多数据");
        notifyDataSetChanged();
    }


    public ArrayList<Integer> getIdList() {
        return mIdList;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1)
            return LOAD_MORE_ITEM;
        if (headerView != null) {
            if (position == 0) return HEADER_ITEM;
            if (position == 1) return GROUP_ITEM;
            else return NORMAL_ITEM;
        } else {
            if (position == 0) return GROUP_ITEM;
            else return NORMAL_ITEM;
        }
    }


    public void changeMoreStatus(int status) {
        Log.e(LOG_TAG, "改变状态");
        this.load_more_status = status;
        Log.e(LOG_TAG, "改变以后状态      " + this.load_more_status + "");
        notifyDataSetChanged();
    }

    public int getStatus() {
        return load_more_status;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEADER_ITEM && headerView != null) {
            return new HeaderViewHolder(headerView);
        } else if (viewType == LOAD_MORE_ITEM) {
            return new LoadMoreViewHolder(mLayoutInflater.inflate(R.layout.item_load_more, parent, false));
        } else if (viewType == NORMAL_ITEM) {
            return new NormalViewHolder(mLayoutInflater.inflate(R.layout
                    .item_news_image, parent, false));
        } else {
            return new GroupViewHolder(mLayoutInflater.inflate(R.layout.item_news_group, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.update(position);

    }


    @Override
    public int getItemCount() {
        return mLastNewsData.size() + 1;
    }


    /**
     * 通用的ViewHolder
     */
    public abstract class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        protected void update(int position) {
        }
    }

    class HeaderViewHolder extends ViewHolder {

        public HeaderViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void update(int position) {
            super.update(position);
        }
    }

    class LoadMoreViewHolder extends ViewHolder {
//        Log.e(Tag)

        @BindView(R.id.icon_loading)
        ProgressWheel mIconLoading;
        @BindView(R.id.icon_finish)
        TextView mIconFinish;

        public LoadMoreViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void update(int position) {
            super.update(position);
            switch (load_more_status) {
                case PULL_UP_LOAD_MORE:
                    mIconFinish.setText("上拉加载数据");
                    mIconLoading.setVisibility(View.GONE);
                    mIconFinish.setVisibility(View.VISIBLE);
                    break;
                case LOADING_MORE:
                    mIconFinish.setText("正在加载更多数据……");
                    mIconLoading.setVisibility(View.VISIBLE);
                    mIconFinish.setVisibility(View.VISIBLE);
                    break;
            }

        }


    }

    class NormalViewHolder extends ViewHolder {
        @BindView(R.id.iv_news_image)
        SimpleDraweeView mIvNewsImage;
        @BindView(R.id.tv_news_text)
        TextView mTvNewsText;

        private LastNewsInfo.StoriesEntity mData;

        public NormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void update(int position) {
            super.update(position);
            mData = mLastNewsData.get(position);
            if (mData.getImages() != null) {
                mIvNewsImage.setVisibility(View.VISIBLE);
                mIvNewsImage.setImageURI(mData.getImages().get(0));
            } else {
                mIvNewsImage.setVisibility(View.GONE);
            }
            mTvNewsText.setText(mData.getTitle());

        }
    }

    class GroupViewHolder extends ViewHolder {
        @BindView(R.id.tv_group_time)
        TextView mTvGroupTime;
        @BindView(R.id.iv_news_image)
        SimpleDraweeView mIvNewsImage;
        @BindView(R.id.tv_news_text)
        TextView mTvNewsText;
        private LastNewsInfo.StoriesEntity mData;

        public GroupViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void update(int position) {
            super.update(position);
            mData = mLastNewsData.get(position);
            String date = DateUtils.FriendlyDate(mLastNewsInfo.getDate());
            if (mData.getImages() != null) {
                mIvNewsImage.setVisibility(View.VISIBLE);
                mIvNewsImage.setImageURI(mData.getImages().get(0));
            } else {
                mIvNewsImage.setVisibility(View.GONE);
            }
            mTvNewsText.setText(mData.getTitle());
            mTvGroupTime.setText(date);
        }
    }


}
